﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Sunlit.House.Articles.Dto;
using Abp.Domain.Repositories;
using Abp.Authorization;
using Abp.Linq.Extensions;
using Abp.Extensions;
using Abp.AutoMapper;
using Microsoft.EntityFrameworkCore;
using Sunlit.House.Authorization;
using Abp.UI;

namespace Sunlit.House.Articles
{
    public class NewsAppService: AsyncCrudAppService<News,NewsDto,long,GetAllNewsInput,CreateNewsInput,UpdateNewsInput>,INewsAppService
    {
        public NewsAppService(IRepository<News, long> repository) : base(repository)
        {
            CreatePermissionName = PermissionNames.Pages_News_Create;
            UpdatePermissionName = PermissionNames.Pages_News_Edit;
            DeletePermissionName = PermissionNames.Pages_News_Delete;
        }
        protected override IQueryable<News> CreateFilteredQuery(GetAllNewsInput input)
        {
            return base.CreateFilteredQuery(input)
                .Include(n => n.NewsCategory)
                .WhereIf(!input.Title.IsNullOrWhiteSpace(), n => n.Title.Contains(input.Title))
                .WhereIf(input.NewsCategoryId.HasValue, n => n.NewsCategoryId == input.NewsCategoryId)
                .WhereIf(input.ShowOnHome.HasValue, n => n.ShowOnHome == input.ShowOnHome)
                .WhereIf(input.IsReprint.HasValue, n => n.IsReprint == input.IsReprint)
                .WhereIf(input.InSlider.HasValue, n => n.InSlider == input.InSlider);
        }
        public async Task AddHitsAsync(EntityDto<long> input)
        {
            await Repository.UpdateAsync(input.Id, async news =>
            {
                news.Hits = news.Hits + 1;
                await Task.CompletedTask;
            });
        }
        public async Task InSlider(EntityDto<long> input)
        {
            CheckUpdatePermission();
            if (Repository.Count(n => n.InSlider) == 6)
            {
                throw new UserFriendlyException("幻灯片中最多显示6条记录！");
            }
            var news = await Repository.GetAsync(input.Id);
            news.InSlider = true;
        }
        public async Task OutSlider(EntityDto<long> input)
        {
            CheckUpdatePermission();
            var news = await Repository.GetAsync(input.Id);
            news.InSlider = false;
        }
        public async Task ShowOnHome(EntityDto<long> input)
        {
            CheckUpdatePermission();
            //if (Repository.Count(n => n.ShowOnHome) == 20)
            //{
            //    throw new UserFriendlyException("首页推荐最多显示20条记录！");
            //}
            var news = await Repository.GetAsync(input.Id);
            news.ShowOnHome = true;
        }
        public async Task HideOnHome(EntityDto<long> input)
        {
            CheckUpdatePermission();
            var news = await Repository.GetAsync(input.Id);
            news.ShowOnHome = false;
        }
        public async Task<List<NewsDto>> GetSliderAsync()
        {
            var list = await Repository.GetAllListAsync(n => n.InSlider);
            return ObjectMapper.Map<List<NewsDto>>(list);
        }
        public override async Task<NewsDto> GetAsync(EntityDto<long> input)
        {
            var news = await Repository.GetAll()
                .Include(n => n.NewsCategory)
                .FirstOrDefaultAsync(n => n.Id == input.Id);
            return ObjectMapper.Map<NewsDto>(news);
        }
    }
}
